<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        body {
            width: 100%;
            height: 5000px;
            position: relative;
        }
        .btn {
            position: absolute;
            top: 4000px;
            width: 300px;
            height: 300px;
            background-color: pink;
            margin: auto;
        }
    </style>
    <button class="btn" style="display: none;">回到顶部</button>
    <script>
        var btn = document.getElementsByTagName('button')[0]
        onscroll = function(){
            console.log(document.documentElement.scrollTop)
            console.log(document.documentElement.scrollTop > 4000 )
            if(document.documentElement.scrollTop >= 4000 ) {
                btn.style.display = 'block'
            }
        }
        btn.onclick = function(){
            var time = setInterval(function(){
                document.documentElement.scrollTop -= 30
                if(document.documentElement.scrollTop <= 0) {
                    clearInterval(time)
                }
            },30)
        }
    </script>
</body>
</html>